package game.module.dabt.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.sql.Connection;
import javax.sql.DataSource;

import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.DaoCommonHandler;
import game.db.DataSourceManager;
import game.module.dabt.bean.DabtBean;

public class DabtdbDao {

	private static Logger logger = LoggerFactory.getLogger(DabtdbDao.class);
	
	private DataSource dataSource = DataSourceManager.getInstance().getDataSource();

	
	static class SingletonHolder{
		static DabtdbDao instance = new DabtdbDao();
	}
	
	public static DabtdbDao getInstance() {
		return SingletonHolder.instance;
	}
	
	private ResultSetHandler<DabtBean> aDabtbean = new ResultSetHandler<DabtBean>(){

		@Override
		public DabtBean handle(ResultSet rs) throws SQLException {
			DabtBean dabtbean = null;
			if(rs.next()) {
				dabtbean = new DabtBean();
				dabtbean.setId(rs.getInt("id"));
				dabtbean.setPlayerid(rs.getInt("playerid"));
				dabtbean.setDabt_money(rs.getInt("dabt_money"));
				dabtbean.setCreditor_id(rs.getInt("creditor_id"));
				dabtbean.setIs_pay(rs.getBoolean("is_pay"));
			}
			return dabtbean;
		}
		
	};
	
	private ResultSetHandler<List<DabtBean>> multiHandler = new ResultSetHandler<List<DabtBean>>() {

		@Override
		public List<DabtBean> handle(ResultSet rs) throws SQLException {
			List<DabtBean> dabtList = new ArrayList<DabtBean>();
			while(rs.next()) {
				DabtBean dabtbean = new DabtBean();
				dabtbean.setId(rs.getInt("id"));
				dabtbean.setPlayerid(rs.getInt("playerid"));
				dabtbean.setDabt_money(rs.getInt("dabt_money"));
				dabtbean.setCreditor_id(rs.getInt("creditor_id"));
				dabtbean.setIs_pay(rs.getBoolean("is_pay"));
				dabtList.add(dabtbean);
			}
			return dabtList;
		}
		
	};
	
	public List<DabtBean> getDabtBeanById(int playerId){
		List<DabtBean> ret = new ArrayList<DabtBean>();
		QueryRunner runner = new QueryRunner(dataSource);
		try {
			ret = runner.query("SELECT * from dabt where playerid = ? and is_pay = false", multiHandler,playerId);
		}catch(SQLException e) {
			logger.error("", e);
			return ret;
		}
		return ret;	
	}
	
	public boolean addDabt(DabtBean dabtbean) {
		boolean addret = false;
		QueryRunner runer = new QueryRunner(dataSource);
		Connection conn = null;
		try {
			conn = dataSource.getConnection();
			int ret = runer.update(conn, "insert into dabt(playerid,dabt_money,creditor_id,is_pay)" + "values(?,?,?,?)",
					dabtbean.getPlayerid(),dabtbean.getDabt_money(),dabtbean.getCreditor_id(),dabtbean.isIs_pay());
			if(ret>0) {
				addret = true;
			}
			if(addret) {
				int theId = runer.query(conn, "SELECT LAST_INSERT_ID();", DaoCommonHandler.Integerhandler);
				dabtbean.setId(theId);
			}
		} catch (SQLException e) {
			logger.error("", e);
		}finally {
			DbUtils.closeQuietly(conn);
		}
		return false;

	}
	
	public void updateDabtIsPay(int id,boolean state) {
		QueryRunner runer = new QueryRunner(dataSource);
		try {
			runer.update("update dabt set is_pay =? where playerid=?",state,id);
		}catch (SQLException e) {
			logger.error("", e);
		}
	}
	public void updateDabtNum(int id,int num) {
		QueryRunner runer = new QueryRunner(dataSource);
		try {
			runer.update("update dabt set dabt_money = dabt_money- ? where playerid=?",num,id);
		}catch (SQLException e) {
			logger.error("", e);
		}
	}
}
